package com.lovo.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lovo.model.Authority;
import com.lovo.service.AuthorityService;
import com.lovo.util.MyUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class AuthorityServiceImpl implements AuthorityService {
    @Override
    public PageInfo<Authority> listByPage(int pageNo, int pageSize,Authority a) {
        SqlSession sqlSession = MyUtils.getSqlSession();
        String stmt = "com.lovo.dao.AuthorityMapper.findAllByAuthorityNameAndAuthorityPath";
        PageHelper.startPage(pageNo,pageSize);
        List<Authority> list = sqlSession.selectList(stmt, a);
        PageInfo<Authority>authorityPageInfo = new PageInfo<>(list);
        sqlSession.commit();
        sqlSession.close();
        return authorityPageInfo;
    }

    @Override
    public boolean addAuthority(Authority a) {
        SqlSession sqlSession = MyUtils.getSqlSession();
        String stmt = "com.lovo.dao.AuthorityMapper.insertSelective";
        int n = sqlSession.insert(stmt, a);
        sqlSession.commit();
        sqlSession.close();
        return n>0?true:false;
    }

    @Override
    public boolean deleteAuthority(int authorityId) {
        SqlSession sqlSession = MyUtils.getSqlSession();
        String stmt = "com.lovo.dao.AuthorityMapper.deleteByPrimaryKey";
        int n = sqlSession.insert(stmt, authorityId);
        sqlSession.commit();
        sqlSession.close();
        return n>0?true:false;
    }

    @Override
    public boolean updateAuthority(Authority a) {
        SqlSession sqlSession = MyUtils.getSqlSession();
        String stmt = "com.lovo.dao.AuthorityMapper.updateByPrimaryKeySelective";
        int n = sqlSession.insert(stmt, a);
        sqlSession.commit();
        sqlSession.close();
        return n>0?true:false;
    }

    @Override
    public List<Authority> getAllAuthority() {
        SqlSession sqlSession = MyUtils.getSqlSession();
        String stmt = "com.lovo.dao.AuthorityMapper.findAll";
        List<Authority> list = sqlSession.selectList(stmt);
        sqlSession.commit();
        sqlSession.close();
        return list;
    }
}
